package net.exoweb.animate.objects
{
    import net.exoweb.animate.objects.DisplayShape;
    import net.exoweb.animate.objects.Vector;

    public class Polygon extends DisplayShape
    {
        private var _thickness:Number;
        private var _thickColor:uint;
        private var _centerAlpha:Number;
        private var _points:Array;

        public function Polygon(thickness:Number=0, thickColor:uint=0x000000, color:uint=0xFFFFFF, alpha:Number=1, bm:String="normal")
        {
            super(color, bm);
            _thickness = thickness;
            _thickColor = thickColor;
            _centerAlpha = alpha;
            _points = new Array();
            display();
        }

        public function set thickness(v:Number):void
        {
            _thickness = v;
            display();
        }

        public function get thickness():Number
        {
            return _thickness;
        }

        public function set thickColor(v:uint):void
        {
            _thickColor = v;
            display();
        }

        public function get thickColor():uint
        {
            return _thickColor;
        }

        public function set centerAlpha(v:Number):void
        {
            _centerAlpha = v;
            display();
        }

        public function get centerAlpha():Number
        {
            return _centerAlpha;
        }

        public function set points(v:Array):void
        {
            if(v.length > 2)
            {
                while(_points.length > 0)
                {
                    _points.pop();
                }
                for(var i:int=0; i<v.length; i++)
                {
                    _points.push(v[i]);
                }
                display();
            }
        }

        public function get points():Array
        {
            return _points;
        }

        public function addPoint(v:Vector):void
        {
            _points.push(v);
        }

        public function removePoints():void
        {
            while(_points.length > 0)
            {
                _points.pop();
            }
        }

        override public function display():void
        {
            graphics.clear();
            if(_points.length > 2)
            {
                if(_thickness != 0)
                {
                    graphics.lineStyle(_thickness, _thickColor);
                }
                graphics.beginFill(_color, _centerAlpha);
                graphics.moveTo(_points[0].x, _points[0].y);
                for(var i:int=1; i<_points.length; i++)
                {
                    graphics.lineTo(_points[i].x, _points[i].y);
                }
                graphics.endFill();
            }
        }
    }
}
